package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    // 任务一：查询用户及角色
    @RequestMapping(value = "/withRoles", method = RequestMethod.GET)
    @ResponseBody
    public User getUserWithRoles(@RequestParam(required = true) String loginname) {
        return userService.getUserWithRoles(loginname);
    }

    // 任务三：多条件查询用户及权限（分页）
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getUsers(
            @RequestParam(required = false) String loginname,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<User> page = userService.getUsersWithRolesAndMenus(loginname, email, status, pageNum, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("rows", page.getResult());
        result.put("pages", page.getPages());
        return result;
    }
}